//
//  DetailViewController.m
//  CloudWeibo
//
//  Created by CLOUD on 12/22/14.
//  Copyright (c) 2014 Cloud. All rights reserved.
//

#import "DetailViewController.h"
#import "WeiBoModel.h"
#import "UIImageView+WebCache.h"
#import "WeiBoView.h"
#import "CommentTableView.h"
#import "CommentModel.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"微博正文";
    _tableView.eventDelegate = self;
    
    [self _initView];
    
    [self loadData];
}

- (void)_initView
{
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    tableHeaderView.backgroundColor = [UIColor clearColor];
    
    
    NSString *userImageUrl = _weiBoModel.user.profile_image_url;
    NSString *userNickName = _weiBoModel.user.screen_name;
    
    self.userImageView.layer.cornerRadius = 5;
    self.userImageView.layer.masksToBounds = YES;
    
    [self.userImageView setImageWithURL:[NSURL URLWithString:userImageUrl]];
    self.nickLabel.text = userNickName;
    
    
    float height = [WeiBoView getWeiboViewHeight:self.weiBoModel isRepost:NO isDetail:YES];
    _weiBoView = [[WeiBoView alloc] initWithFrame:CGRectMake(10, _userBarView.bottom+10, ScreenWidth-20, height)];
    _weiBoView.isDetail = YES;
    _weiBoView.weiBoModel = _weiBoModel;
    
    
    
    [tableHeaderView addSubview:_weiBoView];
    [tableHeaderView addSubview:self.userBarView];
    tableHeaderView.height = (height + 80);
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView release];

    
}

- (void)loadData
{
    NSString *weiBoId = [_weiBoModel.weiBoId stringValue];
    if (weiBoId.length == 0) {
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:weiBoId forKey:@"id"];
    [self.sinaweibo requestWithURL:@"comments/show.json" params:params httpMethod:@"GET" block:^(NSDictionary *ret){
        [self loadDataFinish:ret];
    }];
}

- (void)loadDataFinish:(NSDictionary *) ret
{
    self.tableView.commentDic = ret;
    NSArray *array = [ret objectForKey:@"comments"];
    NSMutableArray *comments = [NSMutableArray arrayWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        CommentModel *commentModel = [[CommentModel alloc] initWithDataDic:dic];
        [comments addObject:commentModel];
        [commentModel release];
    }
    
    if (array.count >= 20) {
        self.tableView.isMore = YES;
    }else{
        self.tableView.isMore = NO;
    }
    
    self.tableView.data = comments;
    [self.tableView reloadData];
    [self.tableView stopPullup];
}


- (void) pullDown:(BaseTableView *) tableView
{
    [tableView performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:2];
}

- (void) pullUp:(BaseTableView *)tableView
{
    [tableView performSelector:@selector(stopPullup) withObject:nil afterDelay:2];
}

- (void) tableView:(BaseTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release];
    [_userImageView release];
    [_nickLabel release];
    [_userBarView release];
    [super dealloc];
}
@end
